import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 041. 滑动窗口的平均值
 * @author: WXY
 * @create: 2022-12-31 14:11
 * @Version 1.0
 **/
public class offer_fourteen1_MovingAverage {
    class MovingAverage {
        int size = 0;
        Queue<Integer> queue;
        double sum;

        /**
         * Initialize your data structure here.
         */
        public MovingAverage(int size) {
            queue = new ArrayDeque<>();
            this.size = size;
            sum = 0;
        }

        public double next(int val) {
            if (queue.size() == size) {
                sum -= queue.poll();
            }
            queue.offer(val);
            sum += val;
            return sum / queue.size();
        }
    }
}
